import numpy as np
import matplotlib.pyplot as plt

# Define condition numbers for f(x) and upper bound of A(x)
def cond_f(x):
    # x = 0处不定义
    return x / (np.exp(x) - 1)

def cond_A(x):
    # x = 0处不定义
    return np.exp(x) / x

# Generate x values for plotting, avoiding x = 0
x_vals = np.linspace(0.1, 1.0, 500)
x_vals = x_vals[x_vals != 0]  # Ensure x != 0 to avoid division by zero

# Calculate condition numbers for each x value
cond_f_vals = np.array([cond_f(x) for x in x_vals])
cond_A_vals = np.array([cond_A(x) for x in x_vals])

# Plot the results
plt.figure(figsize=(10, 8))

# Plot cond_f(x)
plt.plot(x_vals, cond_f_vals, label='$cond_f(x)$')

# Plot cond_A(x)
plt.plot(x_vals, cond_A_vals, label='the Upper Bound of $cond_A(x)$')

# Add labels and title
# Add labels and title
plt.title('Condition Numbers of $cond_f(x)$ and Upper Bound of $cond_A(x)$')
plt.xlabel('$x$')
plt.ylabel('Condition Number Value')
plt.legend(loc='upper right')
plt.grid(True)

# Display the plot
plt.savefig('cond_plot.png', dpi=300) 
plt.show()
